﻿Imports System.Data
Imports System.Data.SqlClient
'Hacemos referencia al proyecto BusinessEntities
Imports BE = BusinessEntities

Public Class PreguntaDAO
    Dim conexion As SqlConnection
    Dim comando As SqlDataAdapter
    Public Shared errores As String
    Dim com As SqlCommand

    'Instanciamos la clase Conexion
    Dim cn As New Conexion

    Public Function ListarPreguntas() As DataSet

        Dim ds As New DataSet
        Try
            conexion = cn.Conectar
            comando = New SqlDataAdapter("sp_listarpreguntas", conexion)
            comando.SelectCommand.CommandType = CommandType.StoredProcedure

            'Dim ds As New DataSet
            comando.Fill(ds, "Preguntas")

        Catch ex As Exception
            errores = ex.Message
        Finally
            conexion.Dispose()
            comando.Dispose()
            ds.Dispose()
        End Try

        Return ds

    End Function

    Public Function BuscarPregunta(ByVal objeto As BE.PreguntaBE) As DataSet

        Dim ds As New DataSet
        Try
            conexion = cn.Conectar
            comando = New SqlDataAdapter("SP_BUSCARPREGUNTA", conexion)
            comando.SelectCommand.CommandType = CommandType.StoredProcedure

            comando.SelectCommand.Parameters.Add(New SqlParameter("@pregunta", SqlDbType.Char, 6))
            comando.SelectCommand.Parameters("@pregunta").Direction = ParameterDirection.Input
            comando.SelectCommand.Parameters("@pregunta").Value = objeto.codigo

            comando.Fill(ds, "LaPregunta")
        Catch ex As Exception
            errores = ex.Message
        Finally
            conexion.Dispose()
            comando.Dispose()
            ds.Dispose()
        End Try

        Return ds


    End Function

    Public Function EliminarPregunta(ByVal objeto As BE.PreguntaBE) As Boolean

        Try
            conexion = cn.Conectar
            com = New SqlCommand("sp_eliminarpregunta", conexion)
            com.CommandType = CommandType.StoredProcedure

            'Definimos los parámetros de entrada

            com.Parameters.Add(New SqlParameter("@pregunta", SqlDbType.Char, 6))
            com.Parameters("@pregunta").Direction = ParameterDirection.Input
            com.Parameters("@pregunta").Value = objeto.codigo

            conexion.Open()
            com.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            errores = ex.Message
            Return False
        Finally
            If conexion.State = ConnectionState.Open Then conexion.Close()
            'Liberamos recursos
            conexion.Dispose()
            com.Dispose()
        End Try

    End Function

    Public Function RegistrarPregunta(ByVal objeto As BE.PreguntaBE) As String
        Try
            conexion = cn.Conectar
            com = New SqlCommand("SP_INSERTARPREGUNTA", conexion)
            com.CommandType = CommandType.StoredProcedure

            'Definimos los parámetros
            com.Parameters.Add(New SqlParameter("@pregunta", SqlDbType.VarChar, 100))
            com.Parameters("@pregunta").Direction = ParameterDirection.Input
            com.Parameters("@pregunta").Value = objeto.tenorpregunta

            com.Parameters.Add(New SqlParameter("@resultado", SqlDbType.VarChar, 100))
            com.Parameters("@resultado").Direction = ParameterDirection.Output

            conexion.Open()
            com.ExecuteNonQuery()
            'Return True
            Return com.Parameters(1).Value

        Catch ex As Exception
            errores = ex.Message
            'Return False
            Return "Error"
        Finally

            If conexion.State = ConnectionState.Open Then conexion.Close()
            'Liberamos recursos
            conexion.Dispose()
            com.Dispose()
        End Try

    End Function

    Public Function ActualizarPregunta(ByVal objeto As BE.PreguntaBE) As String

        Try
            conexion = cn.Conectar
            com = New SqlCommand("SP_ACTUALIZARPREGUNTA", conexion)
            com.CommandType = CommandType.StoredProcedure

            com.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Char, 6))
            com.Parameters("@codigo").Direction = ParameterDirection.Input
            com.Parameters("@codigo").Value = objeto.codigo

            com.Parameters.Add(New SqlParameter("@pregunta", SqlDbType.VarChar, 100))
            com.Parameters("@pregunta").Direction = ParameterDirection.Input
            com.Parameters("@pregunta").Value = objeto.tenorpregunta


            com.Parameters.Add(New SqlParameter("@resultado", SqlDbType.VarChar, 100))
            com.Parameters("@resultado").Direction = ParameterDirection.Output


            conexion.Open()
            com.ExecuteNonQuery()
            'Return True
            Return com.Parameters(2).Value
        Catch ex As Exception
            errores = ex.Message
            'Return False
            Return "Error"
        Finally
            If conexion.State = ConnectionState.Open Then conexion.Close()
            'Liberamos recursos
            conexion.Dispose()
            com.Dispose()
        End Try

    End Function


    Public Function ChapaError() As String
        Return errores
    End Function

End Class
